package com.zff.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zff.dao.XspxGaojianInfoDao;
import com.zff.dao.YsjGaojainDao;
import com.zff.entity.XspxGaojianInfo;
import com.zff.entity.YsjGaojain;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * TODO
 * @version 1.0
 * @author Zff
 * @date 2020/10/29 14:09
 */
@CrossOrigin
@RestController
@ApiOperation("学术评选稿件控制器")
@RequestMapping("/xspxGaojian")
public class XspxGaojianInfoController {
    @Autowired
    private XspxGaojianInfoDao xspxGaojianInfoDao;

    @Autowired
    private YsjGaojainDao ysjGaojainDao;

    /**
     * lw
     * @param userId
     * @return
     */
    @GetMapping("/getGaojianList")
    public String getGaojianList(int userId){
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("type","2");
        map.put("isshow","1");
        List<XspxGaojianInfo> gaojianList = xspxGaojianInfoDao.selectByMap(map);
        System.out.println("gaojianList:"+gaojianList);
        String json= JSON.toJSONString(gaojianList);
        return json;
    }

    /**
     * cg
     * @param userId
     * @return
     */
    @GetMapping("/getGaojianListcg")
    public String getGaojianListcg(int userId){
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("type","1");
        map.put("isshow","1");
        List<XspxGaojianInfo> gaojianList = xspxGaojianInfoDao.selectByMap(map);
        System.out.println("gaojianList:"+gaojianList);
        String json= JSON.toJSONString(gaojianList);
        return json;
    }

    @GetMapping("/deleatGaojian")
    public int deleatGaojian(int id){
        XspxGaojianInfo xspxGaojianInfo = new XspxGaojianInfo();
        xspxGaojianInfo.setId(id);
        xspxGaojianInfo.setIsshow("0");
        int i = xspxGaojianInfoDao.updateById(xspxGaojianInfo);
        return i;
    }

    /**
     * lw
     * @param id
     * @return
     */
    @GetMapping("/getGaojianOne")
    public String getGaojianOne(int id){
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",id);
        XspxGaojianInfo xspxGaojianInfo = xspxGaojianInfoDao.selectById(id);

        String xslw = xspxGaojianInfo.getXslw();
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("filePath",xslw);
        List<YsjGaojain> ysjGaojains = ysjGaojainDao.selectByMap(map1);
        String xslwname = null;
        for (YsjGaojain gaojian: ysjGaojains){
            xslwname = gaojian.getFilename();
        }
        xspxGaojianInfo.setXslwname(xslwname);

        String jxsj = xspxGaojianInfo.getJxsj();
        HashMap<String, Object> map2 = new HashMap<>();
        map2.put("filePath",jxsj);
        List<YsjGaojain> ysjGaojains1 = ysjGaojainDao.selectByMap(map2);
        String jxsjname = null;
        for (YsjGaojain gaojian: ysjGaojains1){
            jxsjname = gaojian.getFilename();
        }
        xspxGaojianInfo.setJsxjname(jxsjname);

        Date birthday = xspxGaojianInfo.getBirthday();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        if (birthday != null){
            String time = ft.format(birthday);
            xspxGaojianInfo.setDday(time);
        }
        String address = xspxGaojianInfo.getAddress();
        String strq = address.substring(1, address.indexOf(","));
        String str1=address.substring(0, address.indexOf(","));
        String str2=address.substring(str1.length()+1, address.length());
        String strh = str2.substring(0, str2.indexOf("]"));
        strq = '"'+strq+'"';
        strh = '"'+strh+'"';
        address = "["+strq+","+strh+"]";
        xspxGaojianInfo.setAddress(address.replace(" ", ""));
        System.out.println("xspxGaojianInfo****************:"+xspxGaojianInfo);
        String json= JSON.toJSONString(xspxGaojianInfo);
        return json;
    }


    @GetMapping("/getGaojianOnecg")
    public String getGaojianOnecg(int id){
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",id);
        XspxGaojianInfo xspxGaojianInfo = xspxGaojianInfoDao.selectById(id);

        String cgzp = xspxGaojianInfo.getCgzp();
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("filePath",cgzp);
        List<YsjGaojain> ysjGaojains = ysjGaojainDao.selectByMap(map1);
        String cgzpname = null;
        for (YsjGaojain gaojian: ysjGaojains){
            cgzpname = gaojian.getFilename();
        }
        xspxGaojianInfo.setCgzpname(cgzpname);

        Date birthday = xspxGaojianInfo.getBirthday();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        if(birthday!=null){
            String time = ft.format(birthday);
            xspxGaojianInfo.setDday(time);
        }

        String address = xspxGaojianInfo.getAddress();
        String strq = address.substring(1, address.indexOf(","));
        String str1=address.substring(0, address.indexOf(","));
        String str2=address.substring(str1.length()+1, address.length());
        String strh = str2.substring(0, str2.indexOf("]"));
        strq = '"'+strq+'"';
        strh = '"'+strh+'"';
        address = "["+strq+","+strh+"]";
        xspxGaojianInfo.setAddress(address.replace(" ", ""));
        System.out.println("xspxGaojianInfo****************:"+xspxGaojianInfo);
        String json= JSON.toJSONString(xspxGaojianInfo);
        return json;
    }

    /**
     * lw
     * @param form
     * @return
     */
    @PostMapping("/updateUser")
    public String updateUserInfo(@RequestBody JSONObject form){
        System.out.println("jjajajaj:"+form);
        Integer userid = (Integer) form.get("userId");
        Integer id = (Integer) form.get("id");
        String username = (String) form.get("username");
        String phone = (String) form.get("phone");
        String sex = (String) form.get("sex");
        String  a= (String)form.get("birthday");
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Date birthday = null;
        if(a!=null){
            try {
                birthday = fmt.parse(a);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        String education = (String) form.get("education");
        String studySection = (String) form.get("studySection");
        String teacherAge = (String) form.get("teacherAge");
        String post = (String) form.get("post");
        String title = (String) form.get("title");
        String workUnit = (String) form.get("workUnit");
        Object aaa = form.get("selectedOptions");
        String address = aaa.toString();
        System.out.println("selectedOptions:"+address);
        String email = (String) form.get("email");
        String xslw = (String) form.get("xslw");
        String jxsj = (String) form.get("jxsj");
        String lwtable = (String) form.get("lwtable");
        String remarks = (String) form.get("remarks");

        String xslwname = null;

        String jxsjname = null;

        XspxGaojianInfo xspxGaojianInfo = new XspxGaojianInfo();
        xspxGaojianInfo.setId(id);
        xspxGaojianInfo.setUserId(userid);
        xspxGaojianInfo.setUsername(username);
        xspxGaojianInfo.setPhone(phone);
        xspxGaojianInfo.setSex(sex);
        xspxGaojianInfo.setBirthday(birthday);
        xspxGaojianInfo.setStudySection(studySection);
        xspxGaojianInfo.setEducation(education);
        xspxGaojianInfo.setTeacherAge(teacherAge);
        xspxGaojianInfo.setPost(post);
        xspxGaojianInfo.setTitle(title);
        xspxGaojianInfo.setWorkUnit(workUnit);
        xspxGaojianInfo.setAddress(address);
        xspxGaojianInfo.setEmail(email);
        xspxGaojianInfo.setRemarks(remarks);
        xspxGaojianInfo.setXslw(xslw);
        xspxGaojianInfo.setLwtable(lwtable);
        if(xslw!=null){
            HashMap<String, Object> map = new HashMap<>();
            map.put("filePath",xslw);
            List<YsjGaojain> ysjGaojains = ysjGaojainDao.selectByMap(map);
            for (YsjGaojain gaojian: ysjGaojains){
                xslwname = gaojian.getFilename();
            }
            xspxGaojianInfo.setXslwname(xslwname);
        }

        xspxGaojianInfo.setJxsj(jxsj);

        if(jxsj!=null){
            HashMap<String, Object> map = new HashMap<>();
            map.put("filePath",jxsj);
            List<YsjGaojain> ysjGaojains = ysjGaojainDao.selectByMap(map);
            for (YsjGaojain gaojian: ysjGaojains){
                jxsjname = gaojian.getFilename();
            }
            xspxGaojianInfo.setJsxjname(jxsjname);
        }
        //xspxGaojianInfo.setResultCode("0");
        int count = xspxGaojianInfoDao.updateById(xspxGaojianInfo);
        System.out.println("countcount:"+count);
        return null;
    }


    /**
     * cg
     * @param form
     * @return
     */
    @PostMapping("/updateUserInfocg")
    public String updateUserInfocg(@RequestBody JSONObject form){
        System.out.println("jjajajaj:"+form);
        Integer userid = (Integer) form.get("userId");
        Integer id = (Integer) form.get("id");
        String username = (String) form.get("username");
        String phone = (String) form.get("phone");
        String sex = (String) form.get("sex");
        String  a= (String)form.get("birthday");
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Date birthday = null;
        if(a!=null){
            try {
                birthday = fmt.parse(a);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        String education = (String) form.get("education");
        String studySection = (String) form.get("studySection");
        String teacherAge = (String) form.get("teacherAge");
        String post = (String) form.get("post");
        String title = (String) form.get("title");
        String workUnit = (String) form.get("workUnit");
        Object aaa = form.get("selectedOptions");
        String address = aaa.toString();
        System.out.println("selectedOptions:"+address);
        String email = (String) form.get("email");
        String cgzp = (String) form.get("cgzp");
        String cgtable = (String) form.get("cgtable");
        String remarks = (String) form.get("remarks");
        String cgzpname = null;


        XspxGaojianInfo xspxGaojianInfo = new XspxGaojianInfo();
        if (cgzp.length()>0 ||cgzp != null){
            xspxGaojianInfo.setResultCode("待评审");
        } else {
            xspxGaojianInfo.setResultCode("请上传作品");
        }
        xspxGaojianInfo.setId(id);
        xspxGaojianInfo.setUserId(userid);
        xspxGaojianInfo.setUsername(username);
        xspxGaojianInfo.setPhone(phone);
        xspxGaojianInfo.setSex(sex);
        xspxGaojianInfo.setBirthday(birthday);
        xspxGaojianInfo.setStudySection(studySection);
        xspxGaojianInfo.setEducation(education);
        xspxGaojianInfo.setTeacherAge(teacherAge);
        xspxGaojianInfo.setPost(post);
        xspxGaojianInfo.setTitle(title);
        xspxGaojianInfo.setWorkUnit(workUnit);
        xspxGaojianInfo.setAddress(address);
        xspxGaojianInfo.setEmail(email);
        xspxGaojianInfo.setRemarks(remarks);
        xspxGaojianInfo.setCgzp(cgzp);
        if(cgzp!=null){
            HashMap<String, Object> map = new HashMap<>();
            map.put("filePath",cgzp);
            List<YsjGaojain> ysjGaojains = ysjGaojainDao.selectByMap(map);
            for (YsjGaojain gaojian: ysjGaojains){
                cgzpname = gaojian.getFilename();
            }
            xspxGaojianInfo.setCgzpname(cgzpname);
        }
        //xspxGaojianInfo.setResultCode("0"getGaojianListcg
        int count = xspxGaojianInfoDao.updateById(xspxGaojianInfo);
        System.out.println("countcount:"+count);
        return null;
    }
}
